import moment from 'moment';
import { RefreshTokenModal } from '../components/refresh-token-modal';
import { ModalHelper } from '../helpers';
import { AppConsts } from '/@/abpPro/AppConsts';
import { abpService, appSessionService } from '/@/shared/abp/';

class RefreshTokenService {
  /**
   * 设置定时器
   */
  public initTimer() {
    const expireTime = abpService.abp.utils.getCookieValue(
      AppConsts.authorization.authTokenExpireDateTime,
    );
    if (expireTime != null) {
      const now = moment();
      const expireSeconds = moment(expireTime);
      if (expireSeconds.isAfter(now)) {
        // 间隔时间
        const tipTime = expireSeconds.subtract(AppConsts.authorization.promptInterval, 'minute');
        const time = moment(tipTime).diff(moment(now), 'seconds') * 1000;
        const timer = setTimeout(() => {
          ModalHelper.create(RefreshTokenModal, {}, { width: 20 }).subscribe((res) => {
            if (res) {
              this.initTimer();
            }
            clearInterval(timer);
          });
        }, time);
      }
    }
    console.log(expireTime);
  }
}
const refreshTokenService = new RefreshTokenService();

export { refreshTokenService };
